package com.mls.fivedistrict.controller.backend;

import com.mls.fivedistrict.common.ServiceResponse;
import com.mls.fivedistrict.controller.BaseController;
import com.mls.fivedistrict.exception.CustomerException;
import com.mls.fivedistrict.pojo.Record;
import com.mls.fivedistrict.pojo.Role;
import com.mls.fivedistrict.pojo.User;
import com.mls.fivedistrict.server.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Optional;

/**
 * Created by chenxiuxiang on 2018/3/19.
 */
@RestController("backendUserController")
@RequestMapping("/api/manager")
public class UserController extends BaseController {
    @Resource(name = "userService")
    UserService userService;
    @Resource(name = "integralService")
    IntegralService integralService;
    @Resource(name = "propertyService")
    PropertyService propertyService;
    @Autowired
    private RoleService roleService;
    @Autowired
    private RolePermissionService rolePermissionService;

    @RequestMapping("/v1/users")
    public ServiceResponse getUserList() {
        return userService.search(getPageFilter());
    }

    @RequestMapping("/v1/user/{userId}")
    public ServiceResponse editUser(@PathVariable("userId") Long userId, @RequestBody User user) {
//        Optional.ofNullable(user.get)
        return userService.editUser(user, userId);
    }


    @PutMapping("/v1/user/addIntegral/{userId}")
    public ServiceResponse addIntegralForUser(@RequestBody Record record, @PathVariable("userId") Long userId) {
        Optional.ofNullable(record.getIntergralId()).orElseThrow(() -> new CustomerException("积分不存在"));
        Optional.ofNullable(record.getSource()).orElseThrow(() -> new CustomerException("来源不存在"));
        Optional.ofNullable(record.getValue()).orElseThrow(() -> new CustomerException("数值不存在"));
        record.setUserId(userId);
        return userService.addPropertyForUser(record, userId);
    }

    @RequestMapping("/v1/user/invitors/{invitorId}")
    public ServiceResponse getInvitorsList(@PathVariable("invitorId") Long invitorId) {
        return userService.getInvitorsByUserId(invitorId, getPageFilter());
    }

    @RequestMapping("/v1/user/userRecordSearch")
    public ServiceResponse getUserRecordList() {
        return userService.getUserRecordList(getPageFilter());
    }

    @RequestMapping("/v1/user/search")
    public ServiceResponse search() {
        ServiceResponse response = userService.search(getPageFilter());
        return response;
    }

    @PutMapping("/v1/user/roles/{userId}")
    public ServiceResponse addRoleForUser(@PathVariable("userId") Long userId, @RequestBody List<Role>roleList) {
//        List<Role> roleList = (List<Role>) params.get("roleList");
        boolean result = userService.editUserRole(roleList, userId);
        if (!result) {
            return ServiceResponse.createErrorResponse("更新失败");
        }
        return ServiceResponse.createSuccessResponse("更新成功");
    }

    @GetMapping("/v1/user/roles/{userId}")
    public ServiceResponse getRoleForUser(@PathVariable("userId") Long userId) {
        List<Role> roleList = roleService.selectRolesByUserId(userId);
        return ServiceResponse.createSuccessResponse(roleList);
    }




}

